package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Override
    public Result submitOrder(Map<String,Object> map) throws Exception {

        Date date = DateUtils.parseString2Date((String) map.get("orderDate"));
        String package_id = (String) map.get("setmealId");
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);
        //校验日期是否在体检日期范围内
        if (null==orderSetting){
            return new Result(false,"该日期没有预约");
        }
        //获取可预约人数
        int number = orderSetting.getNumber();
        //获取已经预约人数
        int reservations = orderSetting.getReservations();

        //判断该日期是否已经预约满
        if (reservations>=number){
            return new Result(false,"该日期预约已满");
        }
        //通过手机号码判断是否是会员,得到member
        String telephone = (String) map.get("telephone");
        //通过手机号码查询出该用户是否是会员
        Member member1 = memberDao.findByTelephone(telephone);


        if (null!=member1){//表示已经是会员
            Integer memberId = member1.getId();
            //通过会员id和预约日期和套餐id来判断是否重复预约了
            Order order = new Order(memberId,date,null,null,Integer.parseInt(package_id));
            List<Order> orderList = orderDao.findByCondition(order);
            if (orderList!=null&&orderList.size()>0){
                return new Result(false,"该日期已经预约了,不能在预约");
            }
        }else {//表示不是会员
            member1 = new Member();
            member1.setName((String) map.get("name"));
            member1.setSex((String) map.get("sex"));
            member1.setIdCard((String)map.get("idCard"));
            member1.setPhoneNumber((String)map.get("telephone"));
            member1.setRegTime(new Date());
            memberDao.add(member1);
        }

        Order order1 = new Order(member1.getId(),date,(String) map.get("orderType"),"未就诊",Integer.parseInt(package_id));
        //可以进行预约了
        orderDao.add(order1);
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return new Result(true,"预约成功",order1.getId());
    }

    @Override
    public Map findById(Integer id) {
        Map map = orderDao.findById4Detail(id);
        return map;
    }


}
